package com.guoliang.wechatpushing.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.guoliang.wechatpushing.service.TranslateService;
import com.guoliang.wechatpushing.utils.RestUtil;
import org.springframework.stereotype.Service;

import javax.swing.table.TableRowSorter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：Leo
 * @date ：Created in 2022/10/18 18:32
 * @description：
 */
@Service
public class TranslateServiceImpl implements TranslateService {
    @Override
    public String translate(String s) {
        //https://fanyi.youdao.com/translate?&doctype=json&type=AUTO&i
        String url="https://fanyi.youdao.com/translate";
        Map<String,Object> map=new HashMap<>();
        map.put("doctype","json");
        map.put("type","AUTO");
        map.put("i",s);
        JSONObject jsonObject = RestUtil.sendGetRequest(RestUtil.getRestTemplate(), url, map, JSONObject.class);
//        System.out.println(jsonObject);
        JSONArray translateResult = jsonObject.getJSONArray("translateResult");
        String tgt = (String)translateResult.getJSONArray(0).getJSONObject(0).get("tgt");
        return tgt;
    }
}
